package com.chapal.ib.client;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.ib.client.Contract;
import com.ib.client.EClientSocket;

public class TWSClient {
	static final Logger logger = Logger.getLogger(TWSClient.class);
	private EClientSocket clientSocket;
	private final String host;
	private final int port;
	private final int clientId;
	Map stockMap;
	Map histStockMap;

	public Map getStockMap() {
		return this.stockMap;
	}

	public void setStockMap(HashMap stockMap) {
		this.stockMap = stockMap;
	}

	public Map getHistStockMap() {
		return this.histStockMap;
	}

	public void setHistStockMap(HashMap histStockMap) {
		this.histStockMap = histStockMap;
	}

	TWSClient(String host, int port, int clientId) {
		this.host = host;
		this.port = port;
		this.clientId = clientId;
	}

	public void setClientSocket(EClientSocket clientSocket) {
		this.clientSocket = clientSocket;
	}

	public void connectToIB() {
		try {
			this.clientSocket.eConnect(this.host, this.port, this.clientId);
		} catch (final Exception e) {
			TWSClient.logger.error(e.getMessage(), e);
		}
	}

	public void reqMktData(int tickerId, Contract contract) {
		this.clientSocket.reqMktData(tickerId, contract, null, false);
	}

	// self.eClientSocket.reqHistoricalData(2, contract, "20100507 12:00:00",
	// "3600 S", "15 secs", "TRADES", 0, 1)
	void reqHistoricalData(int id, Contract contract, String endDateTime, String durationStr, String barSizeSetting, String whatToShow, int useRTH, int formatDate) {
		this.clientSocket.reqHistoricalData(id, contract, endDateTime, durationStr, barSizeSetting, whatToShow, useRTH, formatDate);
	}

	public void start() {
		int noOfTries = 0;

		while (!this.clientSocket.isConnected()) {
			if (noOfTries++ > 10) {
				TWSClient.logger.debug("Unable to connect to tws");
				System.exit(0);
			}
			try {
				Thread.sleep(1000000000);
			} catch (final InterruptedException e) {
				// TODO Auto-generated catch block
				TWSClient.logger.error("Unable to connect to tws", e);
			}

		}
		final Contract c = createNewContract("BAC");
		// clientSocket.reqMktData(1, c,null, true);
		TWSClient.logger.debug("Requesting Historical Data");
		this.clientSocket.reqHistoricalData(1, c, "20100507 12:00:00", "3600 S", "15 secs", "TRADES", 0, 1);
		TWSClient.logger.debug("Requested Historical Data");
		try {
			TWSClient.logger.debug("Before TWSClient.sleep");
			Thread.sleep(3000);
			TWSClient.logger.debug("After TWSClient.sleep");
		} catch (final InterruptedException e) {
			e.printStackTrace();
			TWSClient.logger.error(e.getMessage(), e);
		}
	}

	private Contract createNewContract(String security) {
		final Contract contract = new Contract();
		contract.m_symbol = security;
		contract.m_secType = "STK";
		contract.m_exchange = "SMART";
		contract.m_expiry = "";
		contract.m_right = "";
		contract.m_multiplier = "";
		contract.m_exchange = "SMART";
		contract.m_primaryExch = "";
		contract.m_currency = "USD";
		return contract;
	}

	public void destroy() {
		TWSClient.logger.debug("Disconnecting TWS Connection");
		this.clientSocket.eDisconnect();

	}

}
